package com.yumeng.framework.persistence.mybatis.base.page.param;


import com.yumeng.common.data.annotations.NeedAnon;
import com.yumeng.common.data.annotations.NeedUnknown;
import com.yumeng.common.exception.YumengException;
import com.yumeng.framework.persistence.mybatis.base.page.PageFilterMode;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.core.annotation.AnnotationUtils;

import java.util.List;
import java.util.Map;

/**
 * @author wxd
 * @date 2021/9/7 13:22
 */
@Schema(name = "PageParam", description = "分页参数")
public class PageParam {

    @Schema(description = "当前页，默认 1")
    private Integer pageNum;
    @Schema(description = "每页显示条数，默认 10")
    private Integer pageSize;

    @Schema(hidden = true)
    private Integer total;
    @Schema(hidden = true)
    private Boolean needAnon;
    @Schema(hidden = true)
    private Boolean needUnknown;

    public PageParam(){
        init();
    }
    public PageParam(Integer pageNum, Integer pageSize){
        this();
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    public PageParam(Integer pageNum, Integer pageSize, Integer total){
        this(pageNum, pageSize);
        this.total = total;
    }

    public PageParam(PageParam pageParam){
        this.pageNum = pageParam.getPageNum();
        this.pageSize = pageParam.getPageSize();
        this.total = pageParam.getTotal();
        this.needAnon = pageParam.getNeedAnon();
        this.needUnknown = pageParam.getNeedUnknown();
        this.orders = pageParam.getOrders();
        //通过其他PageParam复制过来，部分属性无法复制 如 pageFilterMode() orderNameMap() 等
    }

    private void init(){
        NeedAnon needAnon = AnnotationUtils.getAnnotation(this.getClass(), NeedAnon.class);
        if (needAnon != null){
            this.needAnon = true;
        }
        NeedUnknown needUnknown = AnnotationUtils.getAnnotation(this.getClass(), NeedUnknown.class);
        if (needUnknown != null){
            this.needUnknown = true;
        }
    }

    /**
     * 移动到下一页，返回是否结束
     * pageNum = 1    pageSize = 20  total = 43
     * @return
     */
    public void moveNextPage(){
        if (total == null || total == 0 || pageNum == null || pageSize == null || pageNum <= 0 || pageSize <= 0){
            throw new YumengException("moveNext 需指定有效total、pageNum、pageSize");
        }
        pageNum++;
    }

    public boolean hasPage(){
        if (total == null || total == 0 || pageNum == null || pageSize == null || pageNum <= 0 || pageSize <= 0){
            throw new YumengException("moveNext 需指定有效total、pageNum、pageSize");
        }
        int pageIndex = pageNum - 1;

        return total > pageIndex * pageSize;
    }

    /**
     * 排序，由前端指定
     */
    @Schema(description = "排序（可多个，英文逗号隔开），格式，属性名_排序标识，asc-正序，desc-倒序，默认正序，如：myname_asc或者age_desc或者myname", type = "String")
    private String orders = null;

    /**
     * 分页过滤模式
     * @return
     */
    public PageFilterMode pageFilterMode(){
        return PageFilterMode.AREA_OR_BUSS;
    }

    //排序字段名称映射<属性名，列名>，只有配置过映射的字段，才支持前端传值排序
    public Map<String, String> orderNameMap(){
        return null;
    }

    /**
     * 默认排序 由后端指定
     * 仅用于mapper模式
     */
    public List<OrderParam> defaultOrders(){
        return null;
    }

    /**
     * 获取默认正序列名序列
     * @return
     */
    public String defaultColNamesAsc(){
        return null;
    }
    /**
     * 获取默认倒序序列名序列
     * @return
     */
    public String defaultColNamesDesc(){
        return null;
    }

    public Integer getPageNum() {
        return pageNum != null ? pageNum : 1;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize != null ? pageSize : 10;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public String getOrders() {
        return orders;
    }

    public void setOrders(String orders) {
        this.orders = orders;
    }

    public Integer getTotal() {
        return total;
    }

    public Boolean getNeedAnon() {
        return needAnon;
    }

    public void setNeedAnon(Boolean needAnon) {
        this.needAnon = needAnon;
    }

    public Boolean getNeedUnknown() {
        return needUnknown;
    }

    public void setNeedUnknown(Boolean needUnknown) {
        this.needUnknown = needUnknown;
    }

//使用构造函数初始化 total，一个逻辑流程内，total不变
/*    public void setTotal(Integer total) {
        this.total = total;
    }*/
}
